// 包含应用中所有接口请求函数的模块
// 每个函数的返回值都是promise

import ajax from './ajax'
import jsonp from 'jsonp'
import { message } from 'antd'


const BASE = ''

// 登录
export const reqLogin = (username, password) => ajax(BASE + '/login', { username, password }, 'POST')

// 天气
export const reqWeather = () => {
  return new Promise((resolve, reject) => {
    const url = 'https://restapi.amap.com/v3/weather/weatherInfo?city=440100&key=7324682973e78a6552854705ef7cdd5b'
    jsonp(url, {}, (err, data) => {
      // 请求成功
      if (!err && data.status === '1') {
        const weather = data.lives[0].weather;
        resolve(weather);
      }
      // 请求失败
      else {
        message.error('获取天气信息失败！')
      }
    })
  })
}

// 获取一级/二级分类的列表// 获取一级/二级分类的列表
export const reqCategorys = (parentId) => ajax(BASE + '/manage/category/list', { parentId })

// 添加分类
export const reqAddCategory = ({ parentId, categoryName }) => ajax(BASE + '/manage/category/add', { parentId, categoryName }, 'POST')

// 修改分类
export const reqUpdateCategory = ({ categoryId, categoryName }) => ajax(BASE + '/manage/category/update', { categoryId, categoryName }, 'POST')

// 获取商品分页列表
export const reqProducts = (pageNum, pageSize) => ajax(BASE + '/manage/product/list', { pageNum, pageSize })

// 添加商品
export const reqAddProduct = (product) => ajax(BASE + '/manage/product/add', product, 'POST')

// 搜索商品分页列表(根据产品名称/商品描述)
// searchType：搜索的类型   productName / productDesc 
export const reqSearchProducts = ({ pageNum, pageSize, searchName, searchType }) => ajax(BASE + '/manage/product/search', { pageNum, pageSize, [searchType]: searchName })

// 获取一个分类
export const reqCategory = (categoryId) => ajax(BASE + '/manage/category/info', { categoryId })

// 更新商品的状态（上架/下架）
export const reqUpdateStatus = (productId, status) => ajax(BASE + '/manage/product/updateStatus', { productId, status }, 'POST')

// 获取所有角色的列表
export const reqRoles = () => ajax(BASE + '/manage/role/list')

// 添加角色
export const reqRoleAdd = (roleName) => ajax(BASE + '/manage/role/add', { roleName }, 'POST')

// 更新角色
export const reqRoleUpdate = (role) => ajax(BASE + '/manage/role/update', { role }, 'POST')

// 获取所有用户列表
export const reqUserList = () => ajax(BASE + '/manage/user/list')

// 添加用户
export const reqUserAdd = (user) => ajax(BASE + '/manage/user/add', user, 'POST')

// 更新用户
export const reqUserUpdate = (user) => ajax(BASE + '/manage/user/update', user, 'POST')

// 删除用户
export const reqUserDel = (userId) => ajax(BASE + '/manage/user/delete', { userId }, 'POST')

// localhost/server_db2